package com.matchmealpha;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class AlertActivity extends Activity {
	
	public static AlertActivity ACTIVE_INSTANCE; 
	public static boolean CURRENTLY_DOWNLOADING; 

	private Button leftButton;
	private Button rightButton;
	private TextView errorMsg;
	private RelativeLayout loadingPanel;

	private ListView alertLayout;
	private List<Alert> alertList;
	protected AlertListAdapter alertAdapter;

	private boolean back;
	private int callerID; // 1 - recent, 2 - alert, 3 - timeline, 4 - friends

	private String username;
	private String sessionid;

	protected ProgressDialog loadingDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		ACTIVE_INSTANCE = this;
		super.onCreate(savedInstanceState);

		/*** Shared Pref ***/
		SharedPreferences settings = getSharedPreferences(
				"matchme_preferences", 0);
		username = settings.getString("username", "");
		sessionid = settings.getString("sessionid", "");

		/*** Saving Bundles Data ***/
		Bundle bund = this.getIntent().getExtras();
		if (bund != null) {
			this.back = bund.getBoolean("back");
			this.callerID = bund.getInt("callerID");
		} else {
			this.back = false;
			this.callerID = MatchMeTabWidget.ALERT;
		}

		/*** Init ***/
		initLayout();

		/*** List View ***/
		alertList = MatchMeTabWidget.alertList;
		MatchMeTabWidget.alertAdapter = new AlertListAdapter(this, alertList, 0, sessionid);
		alertLayout.setAdapter(MatchMeTabWidget.alertAdapter);
		//alertAdapter = new AlertListAdapter(this, alertList, 0, sessionid);
		displayLayout();

//		if(alertList == null)
//		{
//			MatchMeTabWidget.alertList = new  ArrayList<Alert>();
//			alertList = MatchMeTabWidget.alertList;
//			alertLayout.setAdapter(alertAdapter);
//			alertAdapter = new AlertListAdapter(this, alertList, 0, sessionid);
//			/*** Loading Screen ***/
//			loadingDialog = ProgressDialog.show(getParent(), "", "Connecting...",
//					true);
//			loadingDialog.show();
//			new AlertTask().execute();
//			alertAdapter.notifyDataSetChanged();
//		}else
//		{
//			alertLayout.setAdapter(alertAdapter);
//			alertAdapter = new AlertListAdapter(this, alertList, 0, sessionid);
//			displayLayout();
//		}
		
	}

	@Override
	protected void onResume() {
		super.onResume();
		//alertAdapter.notifyDataSetChanged();
	}

	private void initLayout() {
		setContentView(R.layout.alert_view);
		leftButton = (Button) this.findViewById(R.id.AlertLeftButton);
		rightButton = (Button) this.findViewById(R.id.AlertRightButton);
		alertLayout = (ListView) this.findViewById(R.id.AlertListView);
		errorMsg = (TextView) this.findViewById(R.id.AlertError);
		loadingPanel = (RelativeLayout) findViewById(R.id.AlertActivityLoadingPanel);
		
		if(!CURRENTLY_DOWNLOADING){
			 loadingPanel.setVisibility(View.GONE);
		}

		leftButton.setVisibility(View.INVISIBLE);
		rightButton.setVisibility(View.INVISIBLE);
		alertLayout.setVisibility(View.INVISIBLE);
		errorMsg.setVisibility(View.GONE);
	}

	private void displayErrorLayout() {
		errorMsg.setVisibility(View.VISIBLE);
		alertLayout.setVisibility(View.INVISIBLE);
		alertAdapter.notifyDataSetChanged();
		errorMsg.postInvalidate();
	}

	private void displayLayout() {
		errorMsg.setVisibility(View.GONE);
		alertLayout.setVisibility(View.VISIBLE);
		errorMsg.postInvalidate();
	}

	private class AlertTask extends AsyncTask<String, Void, Boolean> {

		protected Boolean doInBackground(String... data) {

			HttpRequest req = new HttpRequest(sessionid);
			String response;
			response = req.sendPost(MatchMeTabWidget.MM_SERVER 
					+ "/get_alerts", "");
			if (MatchMeTabWidget.USER_AUTH_FAILURE.equals(response))
				return false;
			else if (response != null) {
					Scanner in = new Scanner(response).useDelimiter(";");
					Alert alt;
					alertList.clear();
					while (in.hasNext()) {
						alt = new Alert();
						alt.setAlertID(in.nextInt());
						alt.setUserID1(username);
						alt.setUserID2(in.next());
						alt.setType(in.next().toCharArray()[0]);
						alt.setTypeID(in.nextInt());
						alertList.add(alt);
					}
					return true;
			} else
				return false;
		}

		protected void onPostExecute(Boolean result) {
			if(ACTIVE_INSTANCE != null)
			{
				if(ACTIVE_INSTANCE.loadingDialog != null)
				   ACTIVE_INSTANCE.loadingDialog.cancel();
			}
			if (result) {
				if (alertList.size() <= 0) {
					Toast.makeText(getParent(), "No New Alert",
							Toast.LENGTH_LONG);
				}
				displayLayout();
			} else {
				errorMsg.setText("Unable to connect. Please try again");
				displayErrorLayout();
			}
		}
	}

	public void hideLoadingPanel() {
		loadingPanel.setVisibility(View.GONE);	
	}
	public void showLoadingPanel() {
		loadingPanel.setVisibility(View.VISIBLE);	
	}

}
